package com.qigao.mall.web.controller.app.merchant;

import com.qigao.mall.api.MerAdminService;
import com.qigao.mall.commons.dto.MerAdminCreateDto;
import com.qigao.mall.commons.dto.MerAdminDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.MerAdminCreateVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/app/merchant/admin/")
@Api(tags = "App商户管理员")
public class MerchantAdminController {
    private static final Logger logger = LoggerFactory.getLogger(MerchantAdminController.class);
    @Resource
    private MerAdminService merchantAdminService;

    //1.新增管理员
    @PostMapping("/create")
    public Result create(@RequestBody MerAdminCreateVo merAdminCreateVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(merAdminCreateVo);
        try{
            MerAdminCreateDto merAdminCreateDto=new MerAdminCreateDto();
            BeanUtils.copyProperties(merAdminCreateVo,merAdminCreateDto);
            merAdminCreateDto.setOperNo(RequestUtils.getCurrentUserNo(request));
            merchantAdminService.create(merAdminCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("新增管理员发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.启用、禁用管理员
    @PostMapping("/enableDisable/{id}")
    public Result enableDisable(@PathVariable("id")Long id, HttpServletRequest request){
        try{
            merchantAdminService.enableDisable(id,RequestUtils.getCurrentUserNo(request));
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("启用、禁用管理员管理员发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //3.按照商户号查询列表
    @GetMapping("/findListByMerNo/{merNo}")
    public Result findListByMerNo(@PathVariable("merNo")String merNo){
        try{
            List<MerAdminDto> merAdminDtoList=merchantAdminService.findListByMerNo(merNo);
            if(merAdminDtoList==null || merAdminDtoList.size()<=0){
                return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
            }
            return Result.newSuccess(merAdminDtoList);
        }catch (Exception e){
            logger.error("按照商户号查询列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //4.按照用户编号查询商户列表。
    @GetMapping("/findListByUserNo/{userNo}")
    public Result findListByUserNo(@PathVariable("userNo")String userNo){
        try{
            List<MerAdminDto> merAdminDtoList=merchantAdminService.findListByUserNo(userNo);
            if(merAdminDtoList==null || merAdminDtoList.size()<=0){
                return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
            }
            return Result.newSuccess(merAdminDtoList);
        }catch (Exception e){
            logger.error("按照用户编号查询商户列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
